<template name="slide">
  <music-list :title="title" :bg-image="bgImage" :songs="songs" :rank="rank"></music-list>
</template>

<script type="text/ecmascript-6">
  import MusicList from "../music-list/music-list";
  import {mapGetters} from 'vuex'
  import {getMusicList} from "../../api/rank";
  import {ERR_OK} from "../../api/config";
  import {createSong, isValidMusic} from "../../common/js/song";

  let _this = null;

  export default {
    props: [],
    data(){
      return {
        songs:[],
        rank:true
      }
    },
    mounted () {
      _this = this;
      _this.init();
    },
    created(){
      this._getMusicList()
    },
    methods: {
      init () {
        // console.log(this.topList)
      },
      _getMusicList(){
        if(!this.topList.id){
          this.$router.push('/rank')
        }
        getMusicList(this.topList.id).then((res) => {
          if(res.code === ERR_OK){
            // console.log(res.songlist)
            this.songs = this._normalizeSongs(res.songlist)
          }
        })
      },
      _normalizeSongs(list){
        let ret=[]
        list.forEach((item) =>{
          const musicData = item.data
          if(isValidMusic(musicData)){
            ret.push(createSong(musicData))
          }
        })
        return ret
      }
    },
    watch: {},
    components: {MusicList},
    computed: {
      title(){
        return this.topList.topTitle
      },
      bgImage(){
       if(this.songs.length){
         return this.songs[0].image
       }
       return ''
      },
      ...mapGetters([
        'topList'
      ])
    },

  }
</script>


<style lang="stylus" rel="stylesheet/stylus">
  @import '../../common/stylus/variable.styl';
.slide-enter-active,.slide-leave-active
  transition all 0.3s ease
.slide-enter,.slide-leave-to
  transform translate3d(100%,0,0)
</style>
